//
//  ControlTemplateViewRadioController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewRadioController.h"

#pragma mark -
#pragma mark 单选框控件【radio】【option】
@interface ControlTemplateViewRadioController () {
    UILabel* valueLabel;
    CGSize titleSize;
}
@property (nonatomic, retain) NSString* defaultValue;
@end

@implementation ControlTemplateViewRadioController

static ControlTemplateViewRadioController* sharedManagerRadioInter = nil;

+ (ControlTemplateViewRadioController*)sharedManagerRadio
{
    sharedManagerRadioInter = [[self alloc] init];
    
    return sharedManagerRadioInter;
}

/**
 *  单选框变量初始化
 */
- (void)initVar
{
    _valueStr = @"";
    _cnvalueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isNeedAdpterHotspot = YES;
    
    [self initVar];
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [titleLabel setTextColor:kColor_1];
    [titleLabel setFont:kFont_16];
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    titleSize = [[_params valueForKey:@"title"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:TITLE_WIDTH];
    //    CGSize size = STRING_SIZE_FONT(CGRectGetWidth(titleLabel.bounds), [titleLabel text], kFont_16);
    
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"initData"] length] > 0) {
        _defaultValue = [_params valueForKey:@"initData"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    
    //线条
    UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
    lineView.backgroundColor = kColor_L2;
    lineView.tag = 8888;
    [_bgMainView addSubview:lineView];
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    NSArray * initArray = [SEGUtils arrayWithJsonString:[_params valueForKey:@"initData"]];
    NSString * attrValuestring = [_params valueForKey:@"attrValue"];
    NSDictionary * defaultValuedic = nil;
    for (NSDictionary * dic in initArray) {
        NSString * _idStr = [SEGUtils ValueToString:dic[@"id"]];
        if (attrValuestring.length >0) {
            if ([_idStr isEqualToString:attrValuestring]) {
                defaultValuedic = [[NSDictionary alloc] initWithDictionary:dic];
                break;
            }
        }else{
            if ([dic[@"isDefault"] intValue] == 1) {
                defaultValuedic = [[NSDictionary alloc] initWithDictionary:dic];
                break;
            }
        }
    }
    
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) //当writable为0时可读
    {
        UILabel* textLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds), TEMPLATE_HEIGHT)];
        [textLabel setText:defaultValuedic?defaultValuedic[@"value"]:@""];
        [textLabel setTextColor:kColor_1];
        [textLabel setTextAlignment:NSTextAlignmentLeft];
        [textLabel setFont:kFont_16];
        [_bgMainView addSubview:textLabel];
        
    }
    else {
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
        
        CustomButtonView* abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), TEMPLATE_HEIGHT)];
        abtn.tag = 101;
        abtn.tempData = _params;
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:abtn];
        [abtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_bgMainView);
        }];
        //显示值
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - 2 * CONTENTMARGIN_RIGHT - image.size.width, TEMPLATE_HEIGHT)];
        [valueLabel setText:[_params valueForKey:@"markedWords"]];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_C];
        [valueLabel setTextAlignment:NSTextAlignmentLeft];
        [abtn addSubview:valueLabel];
        
        //填充默认值
        if (defaultValuedic) {
            [valueLabel setText:defaultValuedic[@"value"]];
            [valueLabel setTextColor:kColor_1];
            _valueStr = defaultValuedic[@"id"];
            _cnvalueStr = defaultValuedic[@"value"];
            _optional = @"0";
        }
        
        //箭头
        UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(_bgMainView.bounds) - image.size.width - CONTENTMARGIN_RIGHT, (CGRectGetHeight(_bgMainView.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
        iconImageView.tag = 100;
        [iconImageView setImage:image];
        [_bgMainView addSubview:iconImageView];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_bgMainView.mas_centerY);
            make.right.equalTo(_bgMainView.mas_right).offset(-HEADSPACE);
        }];
        
        titleSize.height = titleSize.height > 24 ? titleSize.height : 24;
        valueLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - 2 * CONTENTMARGIN_RIGHT - image.size.width, TEMPLATE_HEIGHT -24 + titleSize.height);
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(valueLabel.bounds));
        UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
        lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    }
    
    //    CGSize sizefont = STRING_SIZE_FONT(CGRectGetWidth(titleLabel.bounds), [titleLabel text], kFont_16);
    //    sizefont.height = titleSize.height > sizefont.height ? titleSize.height : sizefont.height;
    //    CGFloat height = 0;
    //    if (sizefont.height > TEMPLATE_HEIGHT) {
    //        height = sizefont.height;
    //    }
    //    else {
    //        height = TEMPLATE_HEIGHT;
    //    }
    //
    //    _bgMainView.frame = CGRectMake(_bgMainView.frame.origin.x,
    //                                   _bgMainView.frame.origin.y,
    //                                   CGRectGetWidth(_bgMainView.bounds),
    //                                   height);
    //    lineView.frame = CGRectMake(15,
    //                                CGRectGetHeight(_bgMainView.bounds) - 0.5,
    //                                CGRectGetWidth(lineView.bounds),
    //                                0.5);
    //
    //    UIImageView* aimageView = [_bgMainView viewWithTag:100];
    //    aimageView.frame = CGRectMake(aimageView.frame.origin.x,
    //                                  (CGRectGetHeight(_bgMainView.bounds) - CGRectGetHeight(aimageView.bounds)) / 2,
    //                                  CGRectGetWidth(aimageView.bounds),
    //                                  CGRectGetHeight(aimageView.bounds));
    //
    //    CustomButtonView* abtn = [_bgMainView viewWithTag:101];
    //    abtn.frame = CGRectMake(abtn.frame.origin.x,
    //                            0,
    //                            CGRectGetWidth(abtn.bounds),
    //                            CGRectGetHeight(_bgMainView.bounds));
    //
    //    UILabel* aLabel = [abtn.subviews objectAtIndex:0];
    //    aLabel.frame = CGRectMake(aLabel.frame.origin.x,
    //                              0,
    //                              CGRectGetWidth(aLabel.bounds),
    //                              CGRectGetHeight(abtn.bounds));
    //
    //    titleLabel.frame = CGRectMake(titleLabel.frame.origin.x,
    //                                  (CGRectGetHeight(_bgMainView.bounds) - (height + 5)) / 2,
    //                                  CGRectGetWidth(titleLabel.bounds),
    //                                  height + 5);
}

/**
 *  跳入内容选择页
 *
 *  @param sender 按钮时例
 */
- (void)onBtnAction:(CustomButtonView*)sender
{
    
    SEGSpaceViewController* spaceVC = [[SEGSpaceViewController alloc] init];
    spaceVC.fundelegate = self;
    spaceVC.tempDict = _params;
    spaceVC.typeStr = @"radio";
    
    NSString* defaultStr = [SEGUtils ValueToString:_valueStr];
    if (![defaultStr isEqualToString:@"required"]) {
        spaceVC.defaultStr = defaultStr;
    }
    else {
        spaceVC.defaultStr = _defaultValue;
    }
    
    spaceVC.title = [_params valueForKey:@"title"];
    [self.masterVC.navigationController pushViewController:spaceVC animated:YES];
}

/**
 *  单选择选择值后回调方法
 *
 *  @param data 接收任意型值 默认选择的id值
 */
- (void)onRequestData:(id)data
{
    NSArray* dataArray = data;
    NSDictionary * dict = dataArray.lastObject;
    
    for (CustomButtonView* abtn in _bgMainView.subviews) {
        if ([abtn isKindOfClass:[CustomButtonView class]]) {
            UILabel* alabel = [abtn.subviews objectAtIndex:0];
            [alabel setText:ValueToString([dict valueForKey:@"value"])];
            [alabel setTextColor:kColor_1];
        }
    }
    
    _optional = @"0";
    _valueStr = [SEGUtils ValueToString:[dict valueForKey:@"id"]];
    _cnvalueStr = ValueToString([dict valueForKey:@"value"]);
}

/**
 *  给单选框控件填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingRadio_OptionView:(NSString*)valueStr
{
    valueStr = [NSString stringWithFormat:@"%@",valueStr];
    
    if (valueStr.length > 0) {
        _valueStr = valueStr;
        _cnvalueStr = @"";
        
        NSArray * initArray = [SEGUtils arrayWithJsonString:[_params valueForKey:@"initData"]];
        for (NSDictionary * dic in initArray) {
            if ([[SEGUtils ValueToString:dic[@"id"]] isEqualToString:valueStr]) {
                _cnvalueStr = dic[@"value"];
            }
        }
        [valueLabel setText:_cnvalueStr];
        [valueLabel setTextColor:kColor_1];
        _optional = @"0";
    }else {
        _optional = @"1";
    }
    
}

@end

